package android.slc.appdb.sys.service.imp;

import android.slc.appdb.core.ObjectBox;
import android.slc.appdb.sys.entity.CsDictData;
import android.slc.appdb.sys.entity.CsDictData_;
import android.slc.appdb.sys.service.CsDictDataDaoService;

import androidx.annotation.NonNull;

import java.util.List;

import io.objectbox.Box;
import io.objectbox.query.QueryBuilder;

/**
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2021/8/13 14:29
 */
public class CsDictDataDaoServiceImp extends DaoServiceImp<CsDictData> implements CsDictDataDaoService {
    @Override
    protected String getEntityName() {
        return "dictData";
    }

    @Override
    public Box<CsDictData> getBox() {
        return ObjectBox.getBox(CsDictData.class);
    }

    @Override
    protected void checkEntity(CsDictData entity) {
        super.checkEntity(entity);
        CsDictData csDictData = findDictDataByDictType(entity.getDictType(), entity.getDictValue());
        if (csDictData != null && csDictData.getDictCode() != null) {
            entity.setDictCode(csDictData.getDictCode());
        }
    }

    @Override
    public CsDictData findDictDataByDictType(@NonNull String dictType, @NonNull String dictValue) {
        return getBox().query().equal(CsDictData_.dictType, dictType, QueryBuilder.StringOrder.CASE_SENSITIVE).and().equal(CsDictData_.dictValue, dictValue, QueryBuilder.StringOrder.CASE_SENSITIVE).build().findUnique();
    }

    @Override
    public List<CsDictData> findDictDataListByDictType(@NonNull String dictType) {
        return getBox().query().equal(CsDictData_.dictType, dictType, QueryBuilder.StringOrder.CASE_SENSITIVE).build().find();
    }
}
